<?php

require_once './lib/phpexcel/Classes/PHPExcel.php';

$reader = PHPExcel_IOFactory::createReader('Excel5');
$excel = $reader->load('./files/templates/Pendaftar_Beasiswa.xls');
$excel->getProperties()->setCreator("SIMBION")
						->setLastModifiedBy("SIMBION")
						->setTitle("Daftar Pendaftar Beasiswa pada Masa Pendaftaran")
						->setSubject("Daftar Penerima Beasiswa pada Masa Pendaftaran")
						->setDescription("Daftar pendaftar beasiswa pada beasiswa yang dibuka masa pendaftaran")
						->setCategory("Pendaftar Beasiswa");

$baseSheet = $excel->getSheet();
$baseSheet->setTitle('Pendaftar Beasiswa');
$excel->setActiveSheetIndexByName('Pendaftar Beasiswa');

$baseRow = 4;
foreach ($data as $i => $registrant) {
	$row = $baseRow + $i;
	$excel->getActiveSheet()->insertNewRowBefore($row, 1);
	
	$excel->getActiveSheet()->setCellValue('A'.$row, $i+1);
	$excel->getActiveSheet()->getStyle('A'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
	$excel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(false);
	
	$excel->getActiveSheet()->setCellValue('B'.$row, "'".$registrant['number']);
	$excel->getActiveSheet()->getStyle('B'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$excel->getActiveSheet()->getStyle('B'.$row)->getFont()->setBold(false);
	
	$excel->getActiveSheet()->setCellValue('C'.$row, $registrant['student']);
	$excel->getActiveSheet()->getStyle('C'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	$excel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(false);
	
	$excel->getActiveSheet()->setCellValue('D'.$row, $registrant['major']);
	$excel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$excel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(false);
	
	$excel->getActiveSheet()->setCellValue('E'.$row, $registrant['accepted']);
	$excel->getActiveSheet()->getStyle('E'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	$excel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(false);
			
	$excel->getActiveSheet()->setCellValue('F'.$row, $registrant['name']);
	$excel->getActiveSheet()->getStyle('F'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
																->setWrapText(true);
	$excel->getActiveSheet()->getStyle('F'.$row)->getFont()->setBold(false);
	
	$excel->getActiveSheet()->setCellValue('G'.$row, $registrant['details']);
	$excel->getActiveSheet()->getStyle('G'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
																->setWrapText(true);
	$excel->getActiveSheet()->getStyle('G'.$row)->getFont()->setBold(false);
	
	$excel->getActiveSheet()->getRowDimension($row)->setRowHeight(-1);
}

$excel->setActiveSheetIndex(0);

date_default_timezone_set('Asia/Jakarta');
$date = date('Ymd_His');

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Pendaftar_Beasiswa_'.$date.'.xls"');
header('Cache-Control: max-age=0');

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer->save('php://output');
exit;
